$NG.AllReady(function (page, { useValuesChange,useDataIndexChange,useUpdateRows,useUpdateRow }) {
    const mstform = $NG.getCmpApi("p_form_car_rental_m")
    const dgrid = $NG.getCmpApi("p_form_car_rental_d")
    if(page.oType == "add"){
        console.log($NG.getUser());
        const user = $NG.getUser()
        $NG.updateState((updater)=>{
            updater.data.p_form_car_rental_m.setProps({
                bill_name:"租车申请",
                u_ywlx:"车辆运营费",
                u_app_org:{
                    label:user.orgName,
                    value:user.orgID
                },
                creator:{
                    label:user.userName,
                    value:user.userID
                }
            })
        })


        $NG.execServer("getEpmInfoByUserID", {phid:user.userID},function(res){
            console.log(`用户信息查询`,res);
            if(res.count != 0){
                const _data = JSON.parse(res.data)[0].extendObjects
                console.log(_data);
                $NG.updateState((updater)=>{
                    updater.data.p_form_car_rental_m.setProps({
                        u_app_org:{
                            label:user.orgName,
                            value:user.orgID
                        },
                        creator:{
                            label:user.fu_phid,
                            value:user.hem_name
                        },
                        phid_dept:{
                            label:_data.dept_name,
                            value:_data.dept_phid
                        }
                    })
                })
            }
        })
    }
    useValuesChange(({args})=>{
        getProjectInfo()
    },"p_form_car_rental_m.phid_pc")

    useUpdateRows(({args})=>{
        // 汇总金额
        let _count = 0
        args[0].forEach(item=>{
            _count += Number(item.u_vat_amt) || 0
            if(!item.taxrate&&item.taxrate!=0){
                item.taxrate = 0
            }
        })
        if(window.__cache_lsz?._count!==_count){
            if(!window.__cache_lsz){
                window.__cache_lsz={}
            }
            window.__cache_lsz = _count
            $NG.updateState((updater)=>{
                updater.data.p_form_car_rental_m.setProps({
                    u_cnt_amt:_count
                })
            })
        }
    },"p_form_car_rental_d")

    useDataIndexChange(({args})=>{
        getFynameByFyno(args[0].u_cost_pcno,(data)=>{
            args[0].u_cost_pcname = data.u_name
            dgrid.refreshView()
        })
    },"u_cost_pcno")

    const _sum_key=[
        "u_zlyf",
        "u_yz_vat_amt",
        "u_vat_amt",
        "amt",
        "taxrate",
        "u_taxamount",
    ]
    _sum_key.forEach(item=>{
        useDataIndexChange(({args})=>{
            const _arr =  args[0]
            //申请含税金额
            _arr.u_vat_amt = (Number(_arr.u_zlyf) || 0) * (Number(_arr.u_yz_vat_amt)||0)
            _arr.amt = (Number(_arr.u_vat_amt) || 0)/((1 + Number(_arr.taxrate)||0))
            _arr.u_taxamount = (Number(_arr.u_vat_amt)-Number(_arr.amt))
        },item)
    })
    // 项目信息获取
    getProjectInfo()
     function getProjectInfo(isReturn = false, phid = mstform.getItem("phid_pc").getValue(), fun){
        if(!mstform.getItem("phid_pc").getValue()) return 
        try{
            setTimeout(()=>{
                $NG.execServer("getPtInfoByPhid",{phid: phid}, (res)=>{
                    const _data = JSON.parse(res.data)[0].extendObjects
                    if(res.count == 0) return
                    console.log(`获取项目信息`, _data);
                    if(isReturn){
                        fun(_data)
                    } else {
                        $NG.updateState((updater)=>{
                            updater.data.p_form_car_rental_m.setProps({
                                u_pc_no: _data.pc_no, //项目编码
                                u_fycdgs: {
                                    //事业部or组织
                                    value: _data.org_phid,
                                    label: _data.org_name,
                                },
                                u_jzgs: {
                                    //事业部or组织
                                    value: _data.org_phid,
                                    label: _data.org_name,
                                },
                                //项目经理
                                phid_emp:{
                                    label:_data.project_manager_name,
                                    value:_data.project_manager_phid
                                },
                                u_sqgw: {
                                    //售前顾问
                                    value: _data.user_sqgw_phid,
                                    label: _data.user_sqgw_name,
                                },
                                u_regional_center: {
                                    //区域中心
                                    value: _data.regional_center_phid,
                                    label: _data.regional_center_name,
                                },
                                u_product_line: {
                                    //产品线
                                    value: _data.midwife_phid,
                                    label: _data.midwife_name,
                                },
                                u_fycdbm: {
                                    //部门
                                    value: _data.dept_phid,
                                    label: _data.dept_name,
                                },
                                u_pc_type: {//项目类型
                                    value: _data.type_phid,
                                    label: _data.type_name,
                                },
                            })
                        })
                    }
                })
            },100)
        }catch(err){
            $NG.alert("获取项目信息发生失败请检查~")
        }
    }
   
    // 根据费用编码获取费用名称
    function getFynameByFyno(phid,fun){
        $NG.execServer("getFynameByFyno", {phid},function(res){
            console.log(res);
            if(res.count ==0||!phid)return 
            const _data = JSON.parse(res.data)[0].extendObjects
            fun(_data)
        })
    }
});

